<?php

function toDate($timestamp, $format = 'Y-m-d H:i') {
        if (empty($timestamp))
                return NULL;
        return date($format, $timestamp);
}

/**
 * 生成上传附件验证
 * @param $args   参数
 */
function upload_key($args) {
        $auth_key = md5(C("AUTHCODE") . $_SERVER['HTTP_USER_AGENT']);
        $authkey = md5($args . $auth_key);
        return $authkey;
}

/**
 * flash上传初始化
 * 初始化swfupload上传中需要的参数
 * @param $module 模块名称
 * @param $catid 栏目id
 * @param $args 传递参数
 * @param $userid 用户id
 * @param $groupid 用户组id 默认游客
 * @param $isadmin 是否为管理员模式
 */
function initupload($module, $catid, $args, $userid, $groupid = '8', $isadmin = false) {
        //检查用户组上传权限
        if (!$isadmin) {
                $Member_group = F("Member_group");
                if ((int) $Member_group[$groupid]['allowattachment'] < 1 || empty($Member_group)) {
                        return false;
                }
        }

        $sess_id = time();
        $swf_auth_key = md5(C("AUTHCODE") . $sess_id);

        //同时允许的上传个数, 允许上传的文件类型, 是否允许从已上传中选择, 图片高度, 图片宽度,是否添加水印1是
        $args = explode(',', $args);
        //参数补充完整
        if (empty($args[1])) {
                //如果允许上传的文件类型为空，启用网站配置的 uploadallowext
                if ($isadmin) {
                        $args[1] = CONFIG_UPLOADALLOWEXT;
                } else {
                        $args[1] = CONFIG_QTUPLOADALLOWEXT;
                }
        }
        //允许上传后缀处理
        $arr_allowext = explode('|', $args[1]);
        foreach ($arr_allowext as $k => $v) {
                $v = '*.' . $v;
                $array[$k] = $v;
        }
        $upload_allowext = implode(';', $array);

        //允许上传大小
        if ($isadmin) {
                $file_size_limit = intval(CONFIG_UPLOADMAXSIZE);
        } else {
                $file_size_limit = intval(CONFIG_QTUPLOADMAXSIZE);
        }
        //上传个数
        $file_upload_limit = intval($args[0]) ? intval($args[0]) : '8';

        $init = 'var swfu = \'\';
	$(document).ready(function(){
		Wind.use("swfupload",GV.DIMAUB+"statics/js/swfupload/handlers.js",function(){
		      swfu = new SWFUpload({
			flash_url:"/statics/js/swfupload/swfupload.swf?"+Math.random(),
			upload_url:"/index.php?m=Attachments&g=Attachment&a=swfupload",
			file_post_name : "Filedata",
			post_params:{
			                        "SWFUPLOADSESSID":"' . $sess_id . '",
			                        "module":"' . $module . '",
			                        "catid":"' . $catid . '",
			                        "uid":"' . $userid . '",
			                        "isadmin":"' . $isadmin . '",
			                        "groupid":"' . $groupid . '",
			                        "thumb_width":"' . intval($args[3]) . '",
			                        "thumb_height":"' . intval($args[4]) . '",
			                        "watermark_enable":"' . (($args[5] == '') ? 1 : intval($args[5])) . '",
			                        "filetype_post":"' . $args[1] . '",
			                        "swf_auth_key":"' . $swf_auth_key . '"
			},
			file_size_limit:"' . $file_size_limit . 'KB",
			file_types:"' . $upload_allowext . '",
			file_types_description:"All Files",
			file_upload_limit:"' . $file_upload_limit . '",
			custom_settings : {progressTarget : "fsUploadProgress",cancelButtonId : "btnCancel"},
	 
			button_image_url: "",
			button_width: 75,
			button_height: 28,
			button_placeholder_id: "buttonPlaceHolder",
			button_text_style: "",
			button_text_top_padding: 3,
			button_text_left_padding: 12,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,

			file_dialog_start_handler : fileDialogStart,
			file_queued_handler : fileQueued,
			file_queue_error_handler:fileQueueError,
			file_dialog_complete_handler:fileDialogComplete,
			upload_progress_handler:uploadProgress,
			upload_error_handler:uploadError,
			upload_success_handler:uploadSuccess,
			upload_complete_handler:uploadComplete
		      });
		});
	})';
        return $init;
}

// 调用接口服务
function X($name, $params = array(), $domain = 'Service') {
        //创建一个静态变量，用于缓存实例化的对象
        static $_service = array();
        $app = C('DEFAULT_APP');
        //如果已经实例化过，则返回缓存实例化对象
        if (isset($_service[$domain . '_' . $app . '_' . $name]))
                return $_service[$domain . '_' . $app . '_' . $name];
        //载入文件
        $class = $name . $domain;
        import("{$domain}.{$name}{$domain}", APP_PATH . 'Lib');
        //服务不可用时 记录日志 或 抛出异常
        if (class_exists($class)) {
                $obj = new $class($params);
                $_service[$domain . '_' . $app . '_' . $name] = $obj;
                return $obj;
        } else {
                return false;
        }
}

// 实例化服务
function service($name, $params = array()) {
        if (strtolower($name) == 'passport') {
                $name = C("INTERFACE_PASSPORT");
                if (!$name) {
                        $name = "Passport";
                }
        }
        return X($name, $params, 'Service');
}

//调用TagLib类
function TagLib($name, $params = array()) {
        return X($name, $params = array(), 'TagLib');
}